<?php

/*============================================
Classe Iastations
============================================*/

class Iastations extends ModelObject{

	protected static $cached_object_list= array();
	protected $EARTH_STATION_ID;
	protected $LOCAL_SITE_NAME;
	protected $EARTH_STATION_AVAILABILITY;

	protected function __construct( $EARTH_STATION_ID,$LOCAL_SITE_NAME,$EARTH_STATION_AVAILABILITY){
		try{
			
			$this->EARTH_STATION_ID = $EARTH_STATION_ID;
			$this->LOCAL_SITE_NAME = $LOCAL_SITE_NAME;
			$this->EARTH_STATION_AVAILABILITY = $EARTH_STATION_AVAILABILITY;
		}
		catch(Excpetion $e){
			throw new Exception("La construction de l'objet de type get_class($this) a échouée",0,$e);
		}
	}

	public function getEARTHSTATIONID(){
		return $this->EARTH_STATION_ID;
	}

	public function getLOCALSITENAME(){
		return $this->LOCAL_SITE_NAME;
	}

	public function getEARTHSTATIONAVAILABILITY(){
		return $this->EARTH_STATION_AVAILABILITY;
	}

	public function setLOCALSITENAME( $LOCAL_SITE_NAME){
		try{
			$temp = $this->LOCAL_SITE_NAME;
			if($this->LOCAL_SITE_NAME !== $LOCAL_SITE_NAME){
				$this->LOCAL_SITE_NAME = $LOCAL_SITE_NAME;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->LOCAL_SITE_NAME = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setLOCALSITENAME() a échouée",0,$e);
		}
	}

	public function setEARTHSTATIONAVAILABILITY( $EARTH_STATION_AVAILABILITY){
		try{
			$temp = $this->EARTH_STATION_AVAILABILITY;
			if($this->EARTH_STATION_AVAILABILITY !== $EARTH_STATION_AVAILABILITY){
				$this->EARTH_STATION_AVAILABILITY = $EARTH_STATION_AVAILABILITY;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->EARTH_STATION_AVAILABILITY = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setEARTHSTATIONAVAILABILITY() a échouée",0,$e);
		}
	}

	public static function create( $EARTH_STATION_ID,$LOCAL_SITE_NAME,$EARTH_STATION_AVAILABILITY){
		try{
			$iastations = new Iastations($EARTH_STATION_ID, $LOCAL_SITE_NAME, $EARTH_STATION_AVAILABILITY);
			$iastations->insert();
			Iastations::cacheObject($iastations);
			return $iastations;
		}
		catch(Excpetion $e){
			throw new Exception("La création de l'objet de type Iastations a échouée",0,$e);
		}
	}

	public static function get( $EARTH_STATION_ID){
		$dbh = SingletonPDO::getInstance();
		try{
			if(is_null($EARTH_STATION_ID) || $EARTH_STATION_ID === "") {
				return null;
			}
			if(Iastations::existsInCache($EARTH_STATION_ID)) {
				return Iastations::getFromCache($EARTH_STATION_ID);
			}
			$dbh->beginTransaction();
			$sql= 'SELECT * FROM `iastations` WHERE `EARTH_STATION_ID` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$EARTH_STATION_ID));
			if ($row = $stmt->fetch()){
				$iastations = new Iastations( $row['EARTH_STATION_ID'],$row['LOCAL_SITE_NAME'],$row['EARTH_STATION_AVAILABILITY']);
				$iastations->id =  $row['EARTH_STATION_ID'];
				$dbh->commit();
				Iastations::cacheObject($iastations);
				return $iastations;
			}
			else {
				$dbh->commit();
				return null;
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récupération de l'objet ayant l'id ". $EARTH_STATION_ID . " a échouée",0,$e);
		}
	}

	protected function update(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql='UPDATE `iastations` SET `LOCAL_SITE_NAME` = :LOCAL_SITE_NAME, `EARTH_STATION_AVAILABILITY` = :EARTH_STATION_AVAILABILITY WHERE `EARTH_STATION_ID` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':LOCAL_SITE_NAME'=>$this->LOCAL_SITE_NAME, ':EARTH_STATION_AVAILABILITY'=>$this->EARTH_STATION_AVAILABILITY, ':id'=>$this->id));
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La modification de l'objet de type ".get_class($this)." ayant l'id ". $this->id . " a échouée",0,$e);
		}
	}

	public function delete(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql = 'DELETE FROM `iastations` WHERE `EARTH_STATION_ID` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$this->id));
			Iastations::removeObjectFromCache($this->id);
			$this->id = null;
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de l'objet de type ".get_class($this)." ayant l'id ". $this->id. " a échouée",0,$e);
		}
	}

	protected function insert(){
		$dbh = SingletonPDO::getInstance();
		try{
			if (is_null($this->id)) {
				$dbh->beginTransaction();
				$sql='INSERT INTO `iastations` ( `LOCAL_SITE_NAME`, `EARTH_STATION_AVAILABILITY`) VALUES (:LOCAL_SITE_NAME, :EARTH_STATION_AVAILABILITY)';
				$stmt = $dbh->prepare($sql);
				$stmt->execute(array(':LOCAL_SITE_NAME'=>$this->LOCAL_SITE_NAME, ':EARTH_STATION_AVAILABILITY'=>$this->EARTH_STATION_AVAILABILITY));
				$this->id = $dbh->lastInsertId();
				$dbh->commit();
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("L'insertion de l'objet de type ".get_class($this)." a échouée",0,$e);
		}
	}

	public static function getAll( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT * FROM `iastations`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			while ($row = $stmt->fetch()) {
				$obj = null;
				if(Iastations::existsInCache($row['EARTH_STATION_ID'])){
					$obj = Iastations::getFromCache($row['EARTH_STATION_ID']);
				}
				else{
					$obj = new Iastations( $row['EARTH_STATION_ID'],$row['LOCAL_SITE_NAME'],$row['EARTH_STATION_AVAILABILITY']);
				}
				if(!is_null($obj)){
					$obj->id = $row['EARTH_STATION_ID'];
					Iastations::cacheObject($obj);
					$objs[] = $obj;
				}
			}
			$dbh->commit();
			return $objs;
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type Iastations a échouée",0,$e);
		}
	}

	public static function getCount( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT count(*) as count FROM `iastations`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			$count = $stmt->fetch();
			$dbh->commit();
			return $count['count'];
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type Iastations a échouée",0,$e);
		}
	}

	public static function deleteAll( $search_params = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql_delete = 'DELETE FROM `iastations`';
			$sql_select = 'SELECT EARTH_STATION_ID FROM `iastations`';
			$sql_delete .= self::generateWhereClause($search_params);
			$sql_select .= self::generateWhereClause($search_params);
			$stmt_delete = $dbh->prepare($sql_delete);
			$stmt_select = $dbh->prepare($sql_select);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$var_select = $stmt_select->execute($array);
			$tab_to_delete_objects = $stmt_select->fetchAll();
			if(sizeof($tab_to_delete_objects) > 0){
				$var_delete = $stmt_delete->execute($array);
				foreach($tab_to_delete_objects as $ind => $row){
					$existing_object = Iastations::getObjectFromCache($row['EARTH_STATION_ID']);
					if(!is_null($existing_object)){
						$existing_object->id = null;
						Iastations::removeObjectFromCache($row['EARTH_STATION_ID']);
					}
				}
			}
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de la liste d'objets de type Iastations a échouée",0,$e);
		}
	}

}